package com.nulldev.util.fileformats.ftf;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkEvent.EventType;
import javax.swing.event.HyperlinkListener;

import com.nulldev.util.error.ErrorCallback;
import com.nulldev.util.io.FileUtil;
import com.nulldev.util.java.swing.extSwing.eCrashDialog;
import com.nulldev.util.java.swing.extSwing.b64Pane.B64Handler;
import com.nulldev.util.java.swing.extSwing.b64Pane.DataConnection;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.ThemeApplier;
import com.nulldev.util.java.swing.fastEPane.FastEditorKit;
import com.nulldev.util.java.swing.font.FontManager;
import com.nulldev.util.other.ArrayUtils;
import com.nulldev.util.threading.legacy.ThreadExecutor;
import com.nulldev.util.uri.URIUtil2;
import com.nulldev.util.uri.nStreamHandlerFactory;

import javax.swing.JScrollPane;
import javax.swing.JEditorPane;

public class FTFVisualDemo extends JFrame {

	private JPanel contentPane;
	private JEditorPane editorPane;

	/**
	 * Launch the application.
	 */
	// 7nhpZm1QUtAtzUjluKcI9cOUPzlIujb7 
	public static void main(String[] args) {
		nStreamHandlerFactory.install();
		ThreadExecutor.execute(new Runnable() {
			public void run() {
				try {
					FontManager.loadSystemSpecificFonts();
					FTFVisualDemo frame = new FTFVisualDemo();
					ThemeApplier.applyTheme(frame);
					frame.setVisible(true);
					final FTFDocument doc = FTFReader.parse(FileUtil.safeCreate("[redacted]"), new ErrorCallback() {
						
						@Override
						public void onError(Throwable t) {
							final eCrashDialog ecx = eCrashDialog.main(t);
							ecx.setWindowTitle("FTF Demo: Error Occured");
							ecx.setMessageTitle("FTF decoding failed.");
							ecx.waitForOK();
						}
					});
					if (doc != null) {
						frame.editorPane.setText(doc.toHTML());
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FTFVisualDemo() {
		setTitle("FTF Demo");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 713, 386);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		editorPane = new JEditorPane();
		editorPane.setContentType("text/html");
		editorPane.setEditable(false);
		scrollPane.setViewportView(editorPane);
		editorPane.setEditorKit(new FastEditorKit());
		editorPane.addHyperlinkListener(new HyperlinkListener() {
			
			@Override
			public void hyperlinkUpdate(HyperlinkEvent e) {
				if (e.getEventType() == EventType.ACTIVATED) {
					URIUtil2.openLink(e.getURL().toString());
				}
			}
		});

	}

	public JEditorPane getEditorPane() {
		return editorPane;
	}
}
